package net.zjitc.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 2 * @Author: 洪瑶
 * 3 * @Date: 2018/10/10 9:49
 * 4
 */
@WebServlet(name = "CheckServlet",urlPatterns = "/checkServlet")
public class CheckServlet extends HttpServlet {
    private static int WIDTH = 60;
    private static int HEIGHT = 25;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        response.setContentType("image/jpeg");
        ServletOutputStream outputStream = response.getOutputStream();
        //不缓存此图片
        response.setHeader("Pragma","No-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);
        //创建内存图像
        BufferedImage bufferedImage = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        //产生随机验证码
        char[]rands = generateCheckCode();

        //产生图像
        drawBackground(graphics);
        drawRands(graphics,rands);
        //结束绘画
        graphics.dispose();

        //图像输出到客户端
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage,"JPEG",bos);
        byte[]buf = bos.toByteArray();
        response.setContentLength(buf.length);
        bos.writeTo(outputStream);

        //将当前验证码存入session中
        session.setAttribute("loginCheck",new String(rands));
    }

    //生成4个字符的验证码
    private char[]generateCheckCode(){
        //字符表
        String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
        char[]rands = new char[4];
        for (int i = 0;i < 4 ; i++)
        {
            int rand = (int) (Math.random()*36);
            rands[i] = chars.charAt(rand);
        }
        return rands;
    }

    //产生图像
    private void drawRands(Graphics graphics,char[]rands){
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font(null,Font.ITALIC|Font.BOLD,18));
        graphics.drawString(""+rands[0],1,17);
        graphics.drawString(""+rands[1],16,15);
        graphics.drawString(""+rands[2],31,18);
        graphics.drawString(""+rands[3],46,16);
        System.out.println(rands);
    }
    private void drawBackground(Graphics graphics){
        //背景
        graphics.setColor(new Color(0xDCDCDC));
        graphics.fillRect(0,0,WIDTH,HEIGHT);
        //随机产生120个干扰点
        for (int i = 0 ; i < 120;i++)
        {
            int x = (int) (Math.random()*WIDTH);
            int y = (int) (Math.random()*HEIGHT);
            int red = (int) (Math.random()*255);
            int green = (int) (Math.random()*255);
            int blue = (int) (Math.random()*255);
            graphics.setColor(new Color(red,green,blue));
            graphics.drawOval(x,y,1,0);

        }
    }
}
